'''
# @Title:
# @Time : 2022/3/18 11:18
# @File : department.py
# @Software: PyCharm

'''
from Requests.ApiObject.apis.BaseApi import BaseApi
from Requests.ApiObject.utils.file_tools import FileTools


class department(BaseApi):

    def __init__(self):
        yaml_data = FileTools.read_yaml("corp_data")
        corpid = yaml_data.get('corpid').get('daletou')
        corpsecret = yaml_data.get('corpsecret').get('department')
        self.access_token = self.get_access_tooken(corpid, corpsecret)

    def create(self, data):
        '''
        添加部门
        :return:
        '''
        create_url = '/department/create'
        params = {
            "access_token": self.access_token
        }
        r = self.send("POST", create_url, json=data, params=params)
        return r

    def delete(self, depart_id):
        '''
        删除部门
        :return:
        '''
        delete_url = '/department/delete'
        params = {
            "access_token": self.access_token,
            "id": depart_id
        }
        r = self.send("GET", delete_url, params=params)
        return r

    def update(self, data):
        '''
        更新部门信息
        :return:
        '''
        update_url = '/department/update'
        params = {
            "access_token": self.access_token
        }
        r = self.send("POST", update_url, json=data, params=params)
        return r

    def search(self):
        '''
        查询部门
        :return:
        '''
        search_url = '/department/list'
        params = {
            "access_token": self.access_token
        }
        r = self.send("GET", search_url, params=params)
        return r

    def is_in_depart_list(self, department_id):
        '''
        查看部门是否已存在
        :return:
        '''
        r = self.search()
        json_obj = r.json()
        expr = '$..id'
        flag = self.wework_jsonpath(json_obj, expr)
        # 如果没有匹配到数据，直接返回False
        if flag:
            if department_id in flag:
                return True
            else:
                return False
        else:
            return False
